﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace wordLearning
{
    public partial class Form1 : Form
    {
        private MySqlConnection connection;

        public Form1()
        {
            InitializeComponent();
            InitializeDatabase();
        }

        private void InitializeDatabase()
        {
            string connectionString = "server=localhost;user=root;database=worddb;port=3306;password= dong1234 ";
            connection = new MySqlConnection(connectionString);
            connection.Open();
        }

        private void DisplayWord(string word)
        {
            string sql = "SELECT * FROM words WHERE english_word = @word";
            MySqlCommand command = new MySqlCommand(sql, connection);
            command.Parameters.AddWithValue("@word", word);

            using (MySqlDataReader reader = command.ExecuteReader())
            {
                if (reader.Read())
                {
                    string chineseWord = reader.GetString("chinese_meaning");
                    MessageBox.Show(chineseWord, "中文词义");
                }
                else
                {
                    MessageBox.Show("错误", "提示");
                }
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string word = textBox1.Text;
                DisplayWord(word);
                textBox1.Text = "";
            }
        }
    }
}
